<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.order.php 488 2014-01-30 16:31:09Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 488 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:31:09 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_FS_INC.'inc.date_long.php');
require_once(DIR_FS_INC.'inc.address_format.php');
require_once(DIR_FS_INC.'inc.get_country_name.php');
require_once(DIR_FS_INC.'inc.get_zone_code.php');
require_once(DIR_FS_INC.'inc.get_tax_description.php');
require_once(DIR_FS_INC.'inc.get_product_id.php');
require_once(DIR_FS_INC.'inc.parse_product_options.php');
require_once (DIR_FS_INC.'inc.get_shippingtime_functions.php');

class order {
    var $info, $totals, $products, $customer, $delivery, $content_type, $db;
    var $shipping_time_name = '';

    function __construct($order_id = '') {
    	global $price, $db;
		$this->info = array();
		$this->totals = array();
		$this->products = array();
		$this->customer = array();
		$this->delivery = array();
		$this->db = $db;

		if(not_null($order_id))
			$this->query($order_id);
		else
			$this->cart();
    }

    function query($order_id) {

		$order = $this->db->db_query("SELECT
                                   		*
	                                   	FROM
	                                   		".TABLE_ORDERS."
	                                   	WHERE
	                                   		orders_id = ".$this->db->db_prepare($order_id));

		$totals = $this->db->db_query("SELECT
      										*
      									FROM
      										".TABLE_ORDERS_TOTAL."
      									WHERE
      										orders_id = ".$this->db->db_prepare($order_id)."
      									ORDER BY
      										sort_order");

		while(!$totals->EOF) {
			$this->totals[] = array('title' => $totals->fields['title'],
		    	                    'text' => $totals->fields['text'],
		        	                'value' => $totals->fields['value']);
			$totals->MoveNext();
		}

    $order_total = $this->db->db_query("SELECT text, value FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_total'");

		// BOF PayPal Anfang
		$order_tax = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_tax'");
		$pp_order_tax = $order_tax->fields['SUM(value)'];

		$pp_order_disc = 0;
		$order_disc = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_discount'");
		$pp_order_disc += $order_disc->fields['SUM(value)'];

		$pp_order_gs = 0;
		$order_gs = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_coupon'");
		$pp_order_gs -= $order_gs->fields['SUM(value)'];

		$order_gs = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_gv'");
		$pp_order_gs -= $order_gs->fields['SUM(value)'];

		$pp_order_fee = 0;
		$order_fee = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_payment'");
		$pp_order_fee += $order_fee->fields['SUM(value)'];

		$order_fee = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_cod_fee'");
		$pp_order_fee += $order_fee->fields['SUM(value)'];

		$order_fee = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_ps_fee'");
		$pp_order_fee += $order_fee->fields['SUM(value)'];

		$order_fee = $this->db->db_query("SELECT SUM(value) FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_loworderfee'");
		$pp_order_fee += $order_fee->fields['SUM(value)'];
		// EOF PayPal Ende

		$shipping_method = $this->db->db_query("SELECT title, value FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = '".$order_id."' AND class = 'ot_shipping'");

		$order_status = $this->db->db_query("SELECT orders_status_name FROM ".TABLE_ORDERS_STATUS." WHERE orders_status_id = '".$order->fields['orders_status']."' AND language_id = '".$_SESSION['languages_id']."'");

		$countries_id = $this->db->db_query("SELECT countries_id FROM ".TABLE_COUNTRIES." WHERE countries_name = '".$order->fields['customers_country']."'");

		$this->info = array(
              'currency' => $order->fields['currency'],
							'currency_value' => $order->fields['currency_value'],
							'payment_method' => $order->fields['payment_method'],
							'date_purchased' => $order->fields['date_purchased'],
							'orders_status' => $order_status->fields['orders_status_name'],
							'last_modified' => $order->fields['last_modified'],
							'total' => strip_tags($order_total->fields['text']),
							'pp_total' => $order_total->fields['value'],
							'pp_shipping' => $shipping_method->fields['value'],
							'pp_tax' => $pp_order_tax,
							'pp_disc' => $pp_order_disc,
							'pp_gs' => $pp_order_gs,
							'pp_fee' => $pp_order_fee,
							'shipping_method' => ((substr($shipping_method->fields['title'], -1) == ':') ? substr(strip_tags($shipping_method->fields['title']), 0, -1) : strip_tags($shipping_method->fields['title'])),
							'comments' => $order->fields['comments']);

		$this->customer = array('id' => $order->fields['customers_id'],
								'name' => $order->fields['customers_name'],
								'firstname' => $order->fields['customers_firstname'],
								'lastname' => $order->fields['customers_lastname'],
								'csID' => $order->fields['customers_cid'],
								'company' => $order->fields['customers_company'],
								'street_address' => $order->fields['customers_street_address'],
								'suburb' => $order->fields['customers_suburb'],
								'city' => $order->fields['customers_city'],
								'postcode' => $order->fields['customers_postcode'],
								'state' => $order->fields['customers_state'],
								'zone_id' => $order->fields['customers_state'],
								'country' => $order->fields['customers_country'],
								'country_id' => $countries_id->fields['countries_id'],
								'format_id' => $order->fields['customers_address_format_id'],
								'telephone' => $order->fields['customers_telephone'],
								'email_address' => $order->fields['customers_email_address']);

		$this->delivery = array('name' => $order->fields['delivery_name'],
								'firstname' => $order->fields['delivery_firstname'],
								'lastname' => $order->fields['delivery_lastname'],
								'company' => $order->fields['delivery_company'],
								'street_address' => $order->fields['delivery_street_address'],
								'suburb' => $order->fields['delivery_suburb'],
								'city' => $order->fields['delivery_city'],
								'postcode' => $order->fields['delivery_postcode'],
								'state' => $order->fields['delivery_state'],
								'zone_id' => $order->fields['customers_state'],
								'country' => $order->fields['delivery_country'],
								'country_id' => $countries_id->fields['countries_id'],
								'country_iso_2' => $order->fields['delivery_country_iso_code_2'],
								'format_id' => $order->fields['delivery_address_format_id']);

		if(empty($this->delivery['name']) && empty($this->delivery['street_address']))
			$this->delivery = false;

		$this->billing = array('name' => $order->fields['billing_name'],
								'firstname' => $order->fields['billing_firstname'],
								'lastname' => $order->fields['billing_lastname'],
								'company' => $order->fields['billing_company'],
								'street_address' => $order->fields['billing_street_address'],
								'suburb' => $order->fields['billing_suburb'],
								'city' => $order->fields['billing_city'],
								'postcode' => $order->fields['billing_postcode'],
								'state' => $order->fields['billing_state'],
								'zone_id' => $order->fields['customers_state'],
								'country' => $order->fields['billing_country'],
								'country_id' => $countries_id->fields['countries_id'],
								'country_iso_2' => $order->fields['billing_country_iso_code_2'],
								'format_id' => $order->fields['billing_address_format_id']);

		$index = 0;
		$orders_products = $this->db->db_query("
			SELECT 
				op.*, p.products_status 
			FROM 
				".TABLE_ORDERS_PRODUCTS." op
				inner join ".TABLE_PRODUCTS." p on (op.products_id = p.products_id)
			WHERE 
				op.orders_id = ".$this->db->db_prepare($order_id));
		while(!$orders_products->EOF) {
			$options_description = '';
			$products_name_suffix = ($orders_products->fields['products_status'] == 2 ? ' (Hausmodell)' : '');

			if ($orders_products->fields['products_id']!=get_product_id($orders_products->fields['products_id'])) {
				$products_name_suffix = ' (konfiguriert)';
		    if ( ($konfigurator = $_SESSION['cart']->use_konfigurator(true, get_product_id($orders_products->fields['products_id']))) !== false ) {
					$options_description = $konfigurator->get_description(parse_product_options($orders_products->fields['products_id']));
				}
      }

			$this->products[$index] = array(
				'qty' => $orders_products->fields['products_quantity'],
				'id' => $orders_products->fields['products_id'],
				'name' => $orders_products->fields['products_name'] . products_name_suffix,
				'model' => $orders_products->fields['products_model'],
				'options_description' => $options_description,
				'image' => $orders_products->fields['products_image'],
				'tax' => $orders_products->fields['products_tax'],
				'price' => $orders_products->fields['products_price'],
				'shipping_time' => $orders_products->fields['products_shipping_time'],
				'final_price' => $orders_products->fields['final_price']);

			$subindex = 0;
			$attributes = $this->db->db_query("SELECT
													*
												FROM
													".TABLE_ORDERS_PRODUCTS_ATTRIBUTES."
												WHERE
													orders_id = ".$this->db->db_prepare($order_id)."
												AND
													orders_products_id = '".$orders_products->fields['orders_products_id']."'");

			if($attributes->_numOfRows) {
				while(!$attributes->EOF) {
					$this->products[$index]['attributes'][$subindex] = array('option' => $attributes->fields['products_options'],
																			'value' => $attributes->fields['products_options_values'],
																			'prefix' => $attributes->fields['price_prefix'],
																			'price' => $attributes->fields['options_values_price']);
					$subindex++;
					$attributes->MoveNext();
				}
			}

			$this->info['tax_groups']["{$this->products[$index]['tax']}"] = '1';

			$index++;
			$orders_products->MoveNext();
		}
	}

	function getOrderData($oID) {
    	global $price;
		
    	require_once(DIR_FS_INC.'inc.get_attributes_model.php');

    	$order = $this->db->db_query("
				SELECT
					op.products_id, op.orders_products_id,
					op.products_model, op.products_name, op.products_image, 
					op.final_price, op.products_quantity,
					op.products_shipping_time, p.products_status,
					pd.products_description,
					ifnull(pd.products_short_description, pd.products_description) products_short_description
				FROM
					".TABLE_ORDERS_PRODUCTS." op
					inner join ".TABLE_PRODUCTS." p on (op.products_id = p.products_id)
					inner join ".TABLE_PRODUCTS_DESCRIPTION." pd on (op.products_id = pd.products_id)
				WHERE
					orders_id = '".(int)$oID."'");

    $max_shipping_time_ranking = 0;
		$order_data = array ();
		$i = 0;
		while (!$order->EOF) {
    	$max_shipping_time_ranking = max($max_shipping_time_ranking, get_shippingtime_rank('name', $order->fields['products_shipping_time']));

			$product_description = (CHECKOUT_PRODUCT_DETAIL == 'short' ? $order->fields['products_short_description'] : $order->fields['products_description']);
			$options_description = '';
			$products_name_suffix = ($order->fields['products_status'] == 2 ? ' (Hausmodell)' : '');
			if ($order->fields['products_id']!=get_product_id($order->fields['products_id'])) {
				$products_name_suffix = ' (konfiguriert)';
		    if ( ($konfigurator = $_SESSION['cart']->use_konfigurator(true, get_product_id($order->fields['products_id']))) !== false ) {
					$options_description = $konfigurator->get_description(parse_product_options($order->fields['products_id']));
				}
      }
			
			$order_data[$i] = array(
				'PRODUCTS_MODEL' => $order->fields['products_model'],
				'PRODUCTS_NAME' => $order->fields['products_name'] . $products_name_suffix,
				'PRODUCTS_DESCRIPTION' => $product_description,
				'PRODUCTS_OPTIONS_DESCRIPTION' => $options_description,
				'PRODUCTS_IMAGE' => (!empty($order->fields['products_image']) ? HTTP_SERVER.DIR_WS_CATALOG.$order->fields['products_image']:''),
				'PRODUCTS_SHIPPING_TIME' => $order->fields['products_shipping_time'],
				'PRODUCTS_PRICE' => $price->format($order->fields['final_price'], true),
				'PRODUCTS_ATTR_COUNT' => '',
				'PRODUCTS_SINGLE_PRICE' => $price->format($order->fields['final_price']/$order->fields['products_quantity'], true),
				'PRODUCTS_QTY' => $order->fields['products_quantity'],
				'PRODUCTS_ATTRIBUTES' => '');
			
			$attributes = $this->db->db_query("SELECT
													products_options,
													products_options_values,
													price_prefix,
													options_values_price
												FROM
													".TABLE_ORDERS_PRODUCTS_ATTRIBUTES."
												WHERE
													orders_products_id = '".$order->fields['orders_products_id']."'");
			$ii = 0;
			$count_attr = '';
			while(!$attributes->EOF) { $ii++;
				$order_data[$i]['PRODUCTS_ATTRIBUTES'][] = array('ATTRIBUTES_OPTION_NAME' => $attributes->fields['products_options'],
																'ATTRIBUTES_VALUE_NAME' => $attributes->fields['products_options_values'],
																'PRODUCTS_ATTRIBUTES_MODEL' => get_attributes_model($order->fields['products_id'], $attributes->fields['products_options_values'], $attributes->fields['products_options']),
																'PRODUCTS_ATTRIBUTES_PRICE_PREFIX' => $attributes->fields['price_prefix'],
																'PRODUCTS_ATTRIBUTES_SINGLE_PRICE' => $price->format($attributes->fields['options_values_price'], true, $getTaxClass['products_tax_class_id']),
																'PRODUCTS_ATTRIBUTES_QTY' => ($order->fields['products_options'] !='Downloads' ? $order->fields['products_quantity'].'x' : ''));
				
				$count_attr = $ii;
				$attributes->MoveNext();
			}
			$order_data[$i]['PRODUCTS_ATTR_COUNT'] = $count_attr+1;
			$i++;
			$order->MoveNext();
		}

		$this->shipping_time_name = get_shippingtime_name('ranking', $max_shipping_time_ranking);
		return $order_data;
    }

    function getTotalData($oID) {
    	global $price;

		$order_data = $this->db->db_query("SELECT
												title,
												text,
												class,
												value,
												sort_order
											FROM
												".TABLE_ORDERS_TOTAL."
											WHERE
												orders_id='".(int)$oID."'
											ORDER BY
												sort_order ASC");

		$order_total = array();
		while(!$order_data->EOF) {
			$order_total[] = array('TITLE' => $order_data->fields['title'],
									'CLASS' => $order_data->fields['class'],
									'VALUE' => $order_data->fields['value'],
									'TEXT' => $order_data->fields['text']);
			if($order_data->fields['class'] == 'ot_total')
				$total = $order_data->fields['value'];

			$order_data->MoveNext();
		}
		return array('data' => $order_total, 'total' => $total);
    }

	function cart() {
		global $currencies,$price;

		$this->content_type = $_SESSION['cart']->get_content_type();
//echo_e($_SESSION, 'E');
		$customer_address = $this->db->db_query("SELECT
														c.payment_unallowed,
														c.shipping_unallowed,
														c.customers_firstname,
														c.customers_cid,
														c.customers_gender,
														c.customers_lastname,
														c.customers_telephone,
														c.customers_email_address,
														ab.entry_company,
														ab.entry_street_address,
														ab.entry_suburb,
														ab.entry_postcode,
														ab.entry_city,
														ab.entry_zone_id,
														z.zone_name,
														co.countries_id,
														co.countries_name,
														co.countries_iso_code_2,
														co.countries_iso_code_3,
														co.address_format_id,
														ab.entry_state
													FROM
														".TABLE_CUSTOMERS." c,
														".TABLE_ADDRESS_BOOK." ab 
														LEFT JOIN
															".TABLE_ZONES." z ON (ab.entry_zone_id = z.zone_id)
														LEFT JOIN 
															".TABLE_COUNTRIES." co ON (ab.entry_country_id = co.countries_id)
													WHERE
														c.customers_id = '".$_SESSION['customer_id']."'
													AND
														ab.customers_id = '".$_SESSION['customer_id']."'
													AND
														c.customers_default_address_id = ab.address_book_id");

		$shipping_address = $this->db->db_query("SELECT
													ab.entry_firstname,
													ab.entry_lastname,
													ab.entry_company,
													ab.entry_street_address,
													ab.entry_suburb,
													ab.entry_postcode,
													ab.entry_city,
													ab.entry_zone_id,
													z.zone_name,
													ab.entry_country_id,
													c.countries_id,
													c.countries_name,
													c.countries_iso_code_2,
													c.countries_iso_code_3,
													c.address_format_id,
													ab.entry_state
												FROM
													".TABLE_ADDRESS_BOOK." ab
													LEFT JOIN 
														".TABLE_ZONES." z ON (ab.entry_zone_id = z.zone_id)
													LEFT JOIN 
														".TABLE_COUNTRIES." c ON (ab.entry_country_id = c.countries_id)
												WHERE
													ab.customers_id = '".$_SESSION['customer_id']."'
												AND
													ab.address_book_id = '".$_SESSION['sendto']."'");

		$billing_address = $this->db->db_query("SELECT
													ab.entry_firstname,
													ab.entry_lastname,
													ab.entry_company,
													ab.entry_street_address,
													ab.entry_suburb,
													ab.entry_postcode,
													ab.entry_city,
													ab.entry_zone_id,
													z.zone_name,
													ab.entry_country_id,
													c.countries_id,
													c.countries_name,
													c.countries_iso_code_2,
													c.countries_iso_code_3,
													c.address_format_id,
													ab.entry_state
												FROM
													".TABLE_ADDRESS_BOOK." ab
													LEFT JOIN 
														".TABLE_ZONES." z ON (ab.entry_zone_id = z.zone_id)
													LEFT JOIN 
														".TABLE_COUNTRIES." c ON (ab.entry_country_id = c.countries_id)
												WHERE
													ab.customers_id = '".$_SESSION['customer_id']."'
												AND
													ab.address_book_id = '".$_SESSION['billto']."'");

		$tax_address = $this->db->db_query("SELECT
												ab.entry_country_id,
												ab.entry_zone_id
											FROM
												".TABLE_ADDRESS_BOOK." ab
												LEFT JOIN 
													".TABLE_ZONES." z ON (ab.entry_zone_id = z.zone_id)
											WHERE
												ab.customers_id = '".$_SESSION['customer_id']."'
											AND
												ab.address_book_id = '".($this->content_type == 'virtual' ? $_SESSION['billto'] : $_SESSION['sendto'])."'");

		$this->info = array('order_status' => DEFAULT_ORDERS_STATUS_ID,
							'currency' => $_SESSION['currency'],
							'currency_value' => $price->currencies[$_SESSION['currency']]['value'],
							'payment_method' => $_SESSION['payment'],
							'shipping_method' => $_SESSION['shipping']['title'],
							'shipping_cost' => $_SESSION['shipping']['cost'],
							'comments' => $_SESSION['comments'],
							'shipping_class'=>$_SESSION['shipping']['id'],
							'payment_class' => $_SESSION['payment']);

		if(isset($_SESSION['payment']) && is_object($_SESSION['payment'])) {
			$this->info['payment_method'] = $_SESSION['payment']->title;
			$this->info['payment_class'] = $_SESSION['payment']->title;

			if(isset($_SESSION['payment']->order_status) && is_numeric($_SESSION['payment']->order_status) && ($_SESSION['payment']->order_status > 0) )
				$this->info['order_status'] = $_SESSION['payment']->order_status;
		}

		$this->customer = array('firstname' => $customer_address->fields['customers_firstname'],
								'lastname' => $customer_address->fields['customers_lastname'],
								'csID' => $customer_address->fields['customers_cid'],
								'gender' => $customer_address->fields['customers_gender'],
								'company' => $customer_address->fields['entry_company'],
								'street_address' => $customer_address->fields['entry_street_address'],
								'suburb' => $customer_address->fields['entry_suburb'],
								'city' => $customer_address->fields['entry_city'],
								'postcode' => $customer_address->fields['entry_postcode'],
								'state' => ((not_null($customer_address->fields['entry_state'])) ? $customer_address->fields['entry_state'] : $customer_address->fields['zone_name']),
								'zone_id' => $customer_address->fields['entry_zone_id'],
								'country' => array('id' => $customer_address->fields['countries_id'], 
													'title' => $customer_address->fields['countries_name'], 
													'iso_code_2' => $customer_address->fields['countries_iso_code_2'], 
													'iso_code_3' => $customer_address->fields['countries_iso_code_3']),
								'format_id' => $customer_address->fields['address_format_id'],
								'telephone' => $customer_address->fields['customers_telephone'],
								'payment_unallowed' => $customer_address->fields['payment_unallowed'],
								'shipping_unallowed' => $customer_address->fields['shipping_unallowed'],
								'email_address' => $customer_address->fields['customers_email_address']);

		$this->delivery = array('firstname' => $shipping_address->fields['entry_firstname'],
								'lastname' => $shipping_address->fields['entry_lastname'],
								'company' => $shipping_address->fields['entry_company'],
								'street_address' => $shipping_address->fields['entry_street_address'],
								'suburb' => $shipping_address->fields['entry_suburb'],
								'city' => $shipping_address->fields['entry_city'],
								'postcode' => $shipping_address->fields['entry_postcode'],
								'state' => ((not_null($shipping_address->fields['entry_state'])) ? $shipping_address->fields['entry_state'] : $shipping_address->fields['zone_name']),
								'zone_id' => $shipping_address->fields['entry_zone_id'],
								'country' => array('id' => $shipping_address->fields['countries_id'], 
													'title' => $shipping_address->fields['countries_name'], 
													'iso_code_2' => $shipping_address->fields['countries_iso_code_2'], 
													'iso_code_3' => $shipping_address->fields['countries_iso_code_3']),
								'country_id' => $shipping_address->fields['entry_country_id'],
								'format_id' => $shipping_address->fields['address_format_id']);

		$this->billing = array('firstname' => $billing_address->fields['entry_firstname'],
								'lastname' => $billing_address->fields['entry_lastname'],
								'company' => $billing_address->fields['entry_company'],
								'street_address' => $billing_address->fields['entry_street_address'],
								'suburb' => $billing_address->fields['entry_suburb'],
								'city' => $billing_address->fields['entry_city'],
								'postcode' => $billing_address->fields['entry_postcode'],
								'state' => ((not_null($billing_address->fields['entry_state'])) ? $billing_address->fields['entry_state'] : $billing_address->fields['zone_name']),
								'zone_id' => $billing_address->fields['entry_zone_id'],
								'country' => array(	'id' => $billing_address->fields['countries_id'],
													'title' => $billing_address->fields['countries_name'],
													'iso_code_2' => $billing_address->fields['countries_iso_code_2'],
													'iso_code_3' => $billing_address->fields['countries_iso_code_3']),
								'country_id' => $billing_address->fields['entry_country_id'],
								'format_id' => $billing_address->fields['address_format_id']);

		$index = 0;
		$this->tax_discount = array();
		$products = $_SESSION['cart']->get_products();

		for($i=0, $n=sizeof($products); $i<$n; $i++) {
			$products_price = $price->GetPrice($products[$i]['id'], false, ($price->GetGroupPrice($products[$i]['id'], $products[$i]['quantity']) ? $products[$i]['quantity'] : 1), $products[$i]['tax_class_id'],'')+$price->format($_SESSION['cart']->attributes_price($products[$i]['id']),false);

			$this->products[$index] = array_merge(
				$products[$i], array(
					'qty' => $products[$i]['quantity'],
					'tax' => get_tax_rate($products[$i]['tax_class_id'], $tax_address->fields['entry_country_id'], $tax_address->fields['entry_zone_id']),
					'tax_description' => get_tax_description($products[$i]['tax_class_id'], $tax_address->fields['entry_country_id'], $tax_address->fields['entry_zone_id']),
					'price_formated' => $price->format($products[$i]['price'], true),
					'final_price_formated' => $price->format($products[$i]['lot_price'], true)
				)
			);

			if(!empty($products[$i]['attributes'])) {
				$subindex = 0;
				reset($products[$i]['attributes']);
				while (list($option, $value) = each($products[$i]['attributes'])) {
					$attributes = $this->db->db_query("SELECT
															popt.products_options_name,
															poval.products_options_values_name,
															pa.options_values_price,
															pa.attributes_model,
															pa.price_prefix
														FROM
															".TABLE_PRODUCTS_OPTIONS." popt,
															".TABLE_PRODUCTS_OPTIONS_VALUES." poval,
															".TABLE_PRODUCTS_ATTRIBUTES." pa
														WHERE
															pa.products_id = '".$products[$i]['id']."'
														AND
															pa.options_id = '".$option."'
														AND
															pa.options_id = popt.products_options_id
														AND
															pa.options_values_id = '".$value."'
														AND
															pa.options_values_id = poval.products_options_values_id
														AND
															popt.language_id = '".$_SESSION['languages_id']."'
														AND
															poval.language_id = '".$_SESSION['languages_id']."'");

					$this->products[$index]['attributes'][$subindex] = array('attributes_id' => $attributes->fields['products_attributes_id'],
																			'option' => $attributes->fields['products_options_name'],
																			'value' => $attributes->fields['products_options_values_name'],
																			'option_id' => $option,
																			'value_id' => $value,
																			'model' => $attributes->fields['attributes_model'],
																			'prefix' => $attributes->fields['price_prefix'],
																			'price' => $attributes->fields['options_values_price']);

					$subindex++;
				}
			}

			$shown_price = $this->products[$index]['final_price'];
			$this->info['subtotal'] += $shown_price;

			if($_SESSION['customers_status']['customers_status_ot_discount_flag'] == 1)
				$shown_price_tax = $shown_price-($shown_price/100 * $_SESSION['customers_status']['customers_status_ot_discount']);

			$products_tax = $this->products[$index]['tax'];
			$products_tax_description = $this->products[$index]['tax_description'];
			if($_SESSION['customers_status']['customers_status_show_price_tax'] == '1') {
				if($_SESSION['customers_status']['customers_status_ot_discount_flag'] == 1) {
					$this->info['tax'] += $shown_price_tax - ($shown_price_tax / (($products_tax < 10) ? "1.0".str_replace('.', '', $products_tax) : "1.".str_replace('.', '', $products_tax)));
					$this->info['tax_groups'][TAX_ADD_TAX."$products_tax_description"] += (($shown_price_tax /(100+$products_tax)) * $products_tax);

				} else {
					$this->info['tax'] += $shown_price - ($shown_price / (($products_tax < 10) ? "1.0".str_replace('.', '', $products_tax) : "1.".str_replace('.', '', $products_tax)));
					$this->info['tax_groups'][TAX_ADD_TAX."$products_tax_description"] += (($shown_price /(100+$products_tax)) * $products_tax);
				}

			} else {
				if($_SESSION['customers_status']['customers_status_ot_discount_flag'] == 1) {
					$this->tax_discount[$products[$i]['tax_class_id']]+=($shown_price_tax/100) * $products_tax;
					$this->info['tax_groups'][TAX_NO_TAX."$products_tax_description"] += ($shown_price_tax/100) * ($products_tax);
				} else {
					$this->info['tax'] += ($shown_price/100) * ($products_tax);
					$this->info['tax_groups'][TAX_NO_TAX."$products_tax_description"] += ($shown_price/100) * ($products_tax);
				}
			}
			$index++;
		}

		foreach ($this->tax_discount as $value)
			$this->info['tax']+=round($value, $price->get_decimal_places($order->info['currency']));

		if($_SESSION['customers_status']['customers_status_show_price_tax'] == '0') {
			$this->info['total'] = $this->info['subtotal']  + $price->format($this->info['shipping_cost'], false,0,true);
			if($_SESSION['customers_status']['customers_status_ot_discount_flag'] == '1')
				$this->info['total'] -= ($this->info['subtotal'] /100 * $_SESSION['customers_status']['customers_status_ot_discount']);

		} else {
			$this->info['total'] = $this->info['subtotal']  + $price->format($this->info['shipping_cost'], false, 0, true);
			if($_SESSION['customers_status']['customers_status_ot_discount_flag'] == '1')
				$this->info['total'] -= ($this->info['subtotal'] /100 * $_SESSION['customers_status']['customers_status_ot_discount']);
		}
	}
}